﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using EBookReader.Library;
using EBookReader.Library.Config;
using EBookReader.Library.Data;
using EBookReader.Library.Encoder;
using EBookReader.Library.Interface;

namespace EBookReader.UI
{
    public partial class MainForm : Form
    {
        private string currentPath;
        private List<EBookWebsite> siteList;
        private EBookWebsite selectSite;
        private string url;

        public MainForm()
        {
            InitializeComponent();
            currentPath = Environment.CurrentDirectory;
            this.Text = "小说采集器 - By Leven";
            try
            {
                this.InitSiteList();
            }
            catch (Exception e)
            {
                MessageBox.Show(this, e.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Environment.Exit(-1);
            }
        }

        private void InitSiteList()
        {
            string path = currentPath + "\\config\\";
            DirectoryInfo configDir = new DirectoryInfo(path);
            if (!configDir.Exists)
            {
                throw new Exception("Config目录不存在.");
            }
            FileInfo[] configFiles = configDir.GetFiles("*.xml");
            siteList = new List<EBookWebsite>();
            EBookWebsite site;
            System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(typeof(EBookWebsite));
            FileStream fs;
            foreach (FileInfo f in configFiles)
            {
                fs = f.OpenRead();
                site = xs.Deserialize(fs) as EBookWebsite;
                fs.Close();
                if (site != null)
                {
                    siteList.Add(site);
                    this.main_sitelist.Items.Add(site.Name);
                }
            }
        }

        private void CheckSiteList()
        {
            EBookWebsite select = null;
            foreach (EBookWebsite e in siteList)
            {
                if (e.Name.Equals(main_sitelist.SelectedItem))
                {
                    select = e;
                }
            }
            if (select == null)
            {
                throw new Exception("请选中一个可采集站点.");
            }
            else
            {
                selectSite = select;
            }
        }

        private void CheckUrl()
        {
            Regex regex = new Regex("http://.*?(/.*?)*");
            Regex regex2 = new Regex("(.*?\\.)+(/.*?)*");
            if (regex.IsMatch(main_url.Text))
            {
                url = main_url.Text;
            }
            else if (regex2.IsMatch(main_url.Text))
            {
                main_url.Text = "http://" + main_url.Text;
                url = main_url.Text;
            }
            else
            {
                throw new Exception("请输入合法URL.");
            }
        }

        private void main_exit_Click(object sender, EventArgs e)
        {
            AddConfig addForm = new AddConfig();
            addForm.RootPath = currentPath;
            addForm.ShowDialog(this);
        }

        private void main_start_Click(object sender, EventArgs e)
        {
            try
            {
                this.CheckSiteList();
                this.CheckUrl();
                SaveFileDialog saveDlg = new SaveFileDialog();
                saveDlg.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
                if (saveDlg.ShowDialog(this) == DialogResult.OK)
                {
                    Download downForm = new Download();
                    downForm.Website = selectSite;
                    PageInfo page = new PageInfo();
                    page = PageInfo.Parse(url);
                    downForm.StartPage = page;
                    downForm.FileName = saveDlg.FileName;
                    downForm.Url = url;
                    downForm.ShowDialog(this);
                }
            }
            catch (ThreadAbortException)
            {
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AboutBoxReader about = new AboutBoxReader();
            about.ShowDialog(this);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            HelpForm helpForm = new HelpForm();
            helpForm.ShowDialog(this);
        }
    }
}